package cn.edu.djtu.fw.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.edu.djtu.fw.dao.GoodsMapper;
import cn.edu.djtu.fw.model.Goods;
import cn.edu.djtu.fw.model.Vgoods;
import cn.edu.djtu.fw.service.GoodsServiceI;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service("goodsService")
public class GoodsServiceImpl implements GoodsServiceI {
	private GoodsMapper goodsMapper;

	public GoodsMapper getGoodsMapper() {
		return goodsMapper;
	}

	@Autowired
	public void setGoodsMapper(GoodsMapper goodsMapper) {
		this.goodsMapper = goodsMapper;
	}

	@Override
	public boolean delete(int gid) {
		try {
			goodsMapper.deleteByPrimaryKey(gid);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public boolean update(Goods goods) {
		try {
			goodsMapper.updateByPrimaryKey(goods);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public boolean insert(Goods goods) {
		try {
			goodsMapper.insert(goods);
			return true;
		} catch (Exception e) {
		}
		return false;
	}

	@Override
	public Goods getGoodsById(int gid) {
		return goodsMapper.selectByPrimaryKey(gid);
	}

	@Override
	public Map<String, Object> getAllGoods(int page, int rows, Goods goods) {
		try {
			PageHelper.startPage(page, rows, true);
			List<Goods> goodss = goodsMapper.getAllGoods(goods);
			Map<String, Object> goodsMap = new HashMap<String, Object>();
			goodsMap.put("rows", goodss);
			goodsMap.put("total", ((Page<Goods>) goodss).getTotal());
			return goodsMap;
		} catch (Exception e) {
		}
		return null;
	}

	@Override
	public Vgoods getGoodsViewById(int id) {
		return goodsMapper.selectGoodsViewByPrimaryKey(id);
	}
}
